import React, { Component, createRef } from "react";

class App extends Component {
  file = createRef();

  upload = () => {
    // this.file.current表示的input元素
    console.log(this.file.current.files[0]);
  };

  render() {
    // 文件上传的内容是只读的，所以只能使用非受控组件
    return (
      <>
        <h2>文件上传框</h2>
        <input type="file" ref={this.file} />
        <br />
        <button onClick={this.upload}>上传</button>
      </>
    );
  }
}

export default App;
